package com.ittx.dzt.baidu.consumer.area;

import com.google.common.collect.Maps;
import com.ittx.dzt.baidu.constant.BaiduApiConstant;
import com.ittx.dzt.baidu.dto.AreaDataDto;
import com.ittx.dzt.baidu.service.BaiduAreaDownloadSpService;
import com.ittx.dzt.baidu.service.BaiduGetAreaListService;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.sys.response.Result;
import com.ittx.dzt.sys.util.json.JacksonTool;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 区域地址数据消费，省份消费
 * <p>
 *
 * </p>
 *
 * @author wl_code@163.com
 * @version 1.0
 * @date 2025/3/4
 */
@Slf4j
@Component
@RocketMQMessageListener(consumerGroup = BaiduApiConstant.Rocket.BAIDU_AREA_DOWN_GROUP,
		topic = BaiduApiConstant.Rocket.BAIDU_AREA_DOWN_TOPIC)
public class BaiduAreaDownConsumer implements RocketMQListener<String> {
	@Autowired(required = false)
	BaiduAreaDownloadSpService baiduAreaDownloadSpService;

	@Autowired
	BaiduGetAreaListService baiduGetAreaListService;

	@Override
	public void onMessage(String message) {
		try {
			// 单条省份处理消费
			AreaDataDto areaDataDto = JacksonTool.toEntity(message, AreaDataDto.class);
			PlatformApiParamDTO platformApiParam = areaDataDto.getPlatformApiParamDTO();
			HashMap<Object, Object> requestMap = Maps.newHashMap();
			requestMap.put("provinceCode", areaDataDto.getProvinceId());
			requestMap.put("querySubNodeFlag", "1");
			//查询省份下城市
			Result<?> cityListResult = baiduGetAreaListService.doProcess(platformApiParam, requestMap);
			if (cityListResult.hasError()) {
				log.error("百度区域地址数据下载省份[{}] 下城市区域失败推送数据失败:[{}]", message, cityListResult.getMsg());
				return;
			}
			List<Map<String, Object>> cityList = (List<Map<String, Object>>) cityListResult.getData();
			baiduAreaDownloadSpService.sendAreasToDataCenter(platformApiParam, cityList, areaDataDto.getAreaData());
		} catch (Exception e) {
			log.error("百度区域地址数据处理消费者入参:[{}]", message);
			log.error("百度区域地址数据处理请求异常", e);
		}

	}
}
